package com.zhhs.galaxyott.activity.moreMoviesActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.VPInterface.IPresenter;
import com.zhhs.galaxyott.adapters.ListFragmentAdapter;
import com.zhhs.galaxyott.adapters.MoviesPagersAdapter;
import com.zhhs.galaxyott.beans.CategoryBean;
import com.zhhs.galaxyott.beans.HomeMovieBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.CategoryListResponse;
import com.zhhs.galaxyott.httpVolley.response.HomemovieListResponse;
import com.zhhs.galaxyott.utils.Logs;

public class MoreMoviesPresenter implements IPresenter<MoreMoviesHolder> {

    private Activity mContext;
    private MoreMoviesHolder mViewHolder;

    List<CategoryBean> titleData;
    private List<HomeMovieBean> specialData;

    private ListFragmentAdapter mlistAdapter;

    private List<View> mViews;
    protected MoviesPagersAdapter mVpAdapter;

    public MoreMoviesPresenter(Activity mContext) {

        this.mContext = mContext;
    }

    @Override
    public void attachView(MoreMoviesHolder view) {
        this.mViewHolder = view;
    }

    public void loadSpecailData() {
        int page = 2;
        RequestMaker.getHomeMovieList(mContext, page, new ResponseListener<HomemovieListResponse>() {
            @Override
            public void onResponse(HomemovieListResponse result) {

                if (result == null) {
                    return;
                }

                Logs.logD("DEBUG_DT",
                        "this is loadSpecailData-->" + +result.errCode + "size" + result.homeMovieBeans.size());
                if (result.errCode == 0) {
                    specialData = new ArrayList<HomeMovieBean>();
                    specialData = result.homeMovieBeans;

                    if (result.homeMovieBeans.size() >= 2) {

                        Glide.with(mContext).load(result.homeMovieBeans.get(0).getImageUrl())
                                .placeholder(R.drawable.homepage_rec_default).crossFade()
                                .into(MoreMoviesHolder.imageLeft);
                        Glide.with(mContext).load(result.homeMovieBeans.get(1).getImageUrl())
                                .placeholder(R.drawable.homepage_rec_default).crossFade()
                                .into(MoreMoviesHolder.imageRight);
                    }

                    mViewHolder.setSpecialListener(specialData);
                }

            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "this is loadSpecailData-->err");

            }
        });

    }

    public void loadData() {

        RequestMaker.getCategoryList(mContext, new ResponseListener<CategoryListResponse>() {

            @Override
            public void onResponse(CategoryListResponse result) {

                Logs.logD("DEBUG_DT", "this is getCategoryList-->" + +result.errCode + "size" + result.Beans.size()
                        + "pos:" + MoreMoviesActivity.pos);
                if (result.errCode == 0) {

                    titleData = new ArrayList<CategoryBean>();
                    titleData = result.Beans;

                    mViewHolder.mListView.setSelection(MoreMoviesActivity.pos);

                    MoreMoviesActivity.selectId = MoreMoviesActivity.pos;

                    mViewHolder.mListView.setItemsCanFocus(true);
                    mlistAdapter = new ListFragmentAdapter(mContext, titleData, MoreMoviesActivity.pos);
                    mViewHolder.mListView.setAdapter(mlistAdapter);

                    mViews = new ArrayList<View>();
                    mViews.removeAll(mViews);
                    for (int i = 0; i < titleData.size(); i++) {
                        mViews.add(mContext.getLayoutInflater().inflate(R.layout.fragment_more_movies, null));

                    }
                    mVpAdapter = new MoviesPagersAdapter(mContext, mViews, titleData);

                    //  先强制设置到指定页面  
                    try {
                        Field field = mViewHolder.mViewPager.getClass().getField("mCurItem");
                        field.setAccessible(true);
                        field.setInt(mViewHolder.mViewPager, MoreMoviesActivity.pos);
                    } catch (Exception e2) {
                        // TODO: handle exception
                        e2.printStackTrace();
                    }

                    mViewHolder.mViewPager.setAdapter(mVpAdapter);
                    mViewHolder.mViewPager.setCurrentItem(MoreMoviesActivity.pos);

                    mViewHolder.setListener(mVpAdapter);

                }
            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "this is getCategoryList-->err");
            }
        });

    }

    @Override
    public void detachView() {
        this.mViewHolder = null;

    }

}
